package org.abyss.upload.web.controller;

import org.abyss.upload.AjaxUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import edu.tgc.entity.file.FilePath;

public abstract class AjaxUploadController {

    protected AjaxUploadService ajaxUploadService;
    protected String path;

    public AjaxUploadService getAjaxUploadService() {
        return this.ajaxUploadService;
    }

    public abstract String getPath();

    @Autowired
    public void setAjaxUploadService(AjaxUploadService ajaxUploadService) {
        this.ajaxUploadService = ajaxUploadService;
    }

    @ResponseBody
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public void upload(MultipartFile[] files) {
        FilePath path = new FilePath(getPath());
        if (files == null || files.length == 0) {
        } else {
            for (MultipartFile file : files) {
                this.ajaxUploadService.upload(file, path);
            }
        }
    }

}
